﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BasicComponents.com
{
    /// <summary>
    /// Window_SelectDateUtils.xaml 的交互逻辑
    /// </summary>
    public partial class Window_SelectDateUtils : Window
    {
        //返回起始、截止日期
        public delegate void GetBackDate(DateTime dt_star, DateTime dt_end);
        public event GetBackDate getBackDate;
        DateTime today = DateTime.Now;
        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="enableIndex">启用的日期范围，一个6个大小的int数组，内容0代表不启用对应序号的选项，否则代表启用【0当日、1昨日、2当月、3上月、4当季、5自定义】</param>
        /// <param name="today">当前日期范围</param>
        /// <param name="defaultCheckedIndex">默认选择项【0当日、1昨日、2当月、3上月、4当季、5自定义】</param>
        public Window_SelectDateUtils(int[] enableIndex, DateTime today, int defaultCheckedIndex)
        {
            InitializeComponent();
            this.DataContext = this;
            this.today = today;
            //启用项
            int index = 0;
            foreach (int i in enableIndex)
            {
                switch (index)
                {
                    case 0: this.rdbtn_today.IsEnabled = (i != 0); break;
                    case 1: this.rdbtn_lastday.IsEnabled = (i != 0); break;
                    case 2: this.rdbtn_month.IsEnabled = (i != 0); break;
                    case 3: this.rdbtn_lastmonth.IsEnabled = (i != 0); break;
                    case 4: this.rdbtn_dagnji.IsEnabled = (i != 0); break;
                    case 5: this.rdbtn_normal.IsEnabled = (i != 0); break;
                }
                index++;
            }
            //默认勾选
            switch (defaultCheckedIndex)
            {
                case 0: this.rdbtn_today.IsChecked = true; break;
                case 1: this.rdbtn_lastday.IsChecked = true; break;
                case 2: this.rdbtn_month.IsChecked = true; break;
                case 3: this.rdbtn_lastmonth.IsChecked = true; break;
                case 4: this.rdbtn_dagnji.IsChecked = true; break;
                case 5:
                    {
                        this.rdbtn_normal.IsChecked = true;
                    }
                    break;
            }
        }
        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="enableIndex">启用的日期范围，一个6个大小的int数组，内容0代表不启用对应序号的选项，否则代表启用【0当日、1昨日、2当月、3上月、4当季、5自定义】</param>
        /// <param name="today">当前日期范围</param>
        /// <param name="defaultCheckedIndex">默认选择项【0当日、1昨日、2当月、3上月、4当季、5自定义】</param>
        /// <param name="defaultStartDate">默认起始日期，传递参数defaultCheckedIndex为5时有效</param>
        /// <param name="defaultEndDate">默认截止日期，传递参数defaultCheckedIndex为5时有效</param>
        public Window_SelectDateUtils(int[] enableIndex, DateTime today, int defaultCheckedIndex, DateTime defaultStartDate, DateTime defaultEndDate)
        {
            InitializeComponent();
            this.DataContext = this;
            this.today = today;
            //启用项
            int index = 0;
            foreach (int i in enableIndex)
            {
                switch (index)
                {
                    case 0: this.rdbtn_today.IsEnabled = (i != 0); break;
                    case 1: this.rdbtn_lastday.IsEnabled = (i != 0); break;
                    case 2: this.rdbtn_month.IsEnabled = (i != 0); break;
                    case 3: this.rdbtn_lastmonth.IsEnabled = (i != 0); break;
                    case 4: this.rdbtn_dagnji.IsEnabled = (i != 0); break;
                    case 5: this.rdbtn_normal.IsEnabled = (i != 0); break;
                }
                index++;
            }
            //默认勾选
            switch (defaultCheckedIndex)
            {
                case 0: this.rdbtn_today.IsChecked = true; break;
                case 1: this.rdbtn_lastday.IsChecked = true; break;
                case 2: this.rdbtn_month.IsChecked = true; break;
                case 3: this.rdbtn_lastmonth.IsChecked = true; break;
                case 4: this.rdbtn_dagnji.IsChecked = true; break;
                case 5:
                    {
                        this.rdbtn_normal.IsChecked = true;
                        if (defaultStartDate != null) this.dt_start.SelectedDate = defaultStartDate;
                        if (defaultEndDate != null) this.dt_end.SelectedDate = defaultEndDate;
                    }
                    break;
            }
        }
        /// <summary>
        /// 初始化控件
        /// </summary>
        /// <param name="enableIndex">启用的日期范围，一个6个大小的int数组，内容0代表不启用对应序号的选项，否则代表启用【0当日、1昨日、2当月、3上月、4当季、5自定义】</param>
        /// <param name="today">当前日期范围</param>
        /// <param name="defaultCheckedIndex">默认选择项【0当日、1昨日、2当月、3上月、4当季、5自定义】</param>
        /// <param name="defaultStartDate">默认起始日期，传递参数defaultCheckedIndex为5时有效</param>
        /// <param name="defaultEndDate">默认截止日期，传递参数defaultCheckedIndex为5时有效</param>
        /// <param name="minDisplayDate">显示的最小日期</param>
        /// <param name="maxDisplayDate">显示的最大日期</param>
        public Window_SelectDateUtils(int[] enableIndex, DateTime today, int defaultCheckedIndex, DateTime defaultStartDate, DateTime defaultEndDate, DateTime minDisplayDate, DateTime maxDisplayDate)
        {
            InitializeComponent();
            this.DataContext = this;
            this.today = today;
            //最小和最大日期范围
            if (minDisplayDate != null) this.dt_start.DisplayDateStart = minDisplayDate;
            if (minDisplayDate != null) this.dt_end.DisplayDateStart = minDisplayDate;
            if (maxDisplayDate != null) this.dt_start.DisplayDateEnd = maxDisplayDate;
            if (maxDisplayDate != null) this.dt_end.DisplayDateEnd = maxDisplayDate;
            //启用项
            int index = 0;
            foreach (int i in enableIndex)
            {
                switch (index)
                {
                    case 0: this.rdbtn_today.IsEnabled = (i != 0); break;
                    case 1: this.rdbtn_lastday.IsEnabled = (i != 0); break;
                    case 2: this.rdbtn_month.IsEnabled = (i != 0); break;
                    case 3: this.rdbtn_lastmonth.IsEnabled = (i != 0); break;
                    case 4: this.rdbtn_dagnji.IsEnabled = (i != 0); break;
                    case 5: this.rdbtn_normal.IsEnabled = (i != 0); break;
                }
                index++;
            }
            //默认勾选
            switch (defaultCheckedIndex)
            {
                case 0: this.rdbtn_today.IsChecked = true; break;
                case 1: this.rdbtn_lastday.IsChecked = true; break;
                case 2: this.rdbtn_month.IsChecked = true; break;
                case 3: this.rdbtn_lastmonth.IsChecked = true; break;
                case 4: this.rdbtn_dagnji.IsChecked = true; break;
                case 5:
                    {
                        this.rdbtn_normal.IsChecked = true;
                        if (defaultStartDate != null) this.dt_start.SelectedDate = defaultStartDate;
                        if (defaultEndDate != null) this.dt_end.SelectedDate = defaultEndDate;
                    }
                    break;
            }
        }

        private void Btn_ok_Click(object sender, RoutedEventArgs e)
        {
            if (!this.dt_start.SelectedDate.HasValue || !this.dt_end.SelectedDate.HasValue)
            {
                MessageBox.Show("起始日期不允许为空，请选择！", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                return;
            }
            else
            {
                if (getBackDate != null)
                {
                    getBackDate(this.dt_start.SelectedDate.Value, this.dt_end.SelectedDate.Value);
                    this.Close();
                }
                else
                {
                    MessageBox.Show("调用者没有实现回调事件，无法返回起止日期！", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
                    return;
                }
            }
        }

        private void Rdbtn_Checked(object sender, RoutedEventArgs e)
        {
            try
            {
                RadioButton rdbtn = sender as RadioButton;
                switch (rdbtn.Tag.ToString())
                {
                    case "today":
                        {
                            this.dt_start.SelectedDate = today;
                            this.dt_end.SelectedDate = today;
                        }; break;
                    case "lastday":
                        {
                            this.dt_start.SelectedDate = today.AddDays(-1);
                            this.dt_end.SelectedDate = today.AddDays(-1);
                        }; break;
                    case "month":
                        {
                            this.dt_start.SelectedDate = today.AddDays(1 - today.Day);
                            this.dt_end.SelectedDate = today;
                        }; break;
                    case "lastmonth":
                        {
                            this.dt_start.SelectedDate = today.AddMonths(-1);
                            this.dt_start.SelectedDate = this.dt_start.SelectedDate.Value.AddDays(1 - this.dt_start.SelectedDate.Value.Day); 
                            this.dt_end.SelectedDate = this.dt_end.SelectedDate.Value.AddDays(- this.dt_end.SelectedDate.Value.Day);
                        }; break;
                    case "dangji":
                        {
                            int month = today.Month;
                            if (month < 3)
                            {
                                this.dt_start.SelectedDate = today.AddMonths(-month);
                                this.dt_start.SelectedDate = this.dt_start.SelectedDate.Value.AddDays(1 - this.dt_start.SelectedDate.Value.Day);
                            }
                            else
                            if (month < 6)
                            {
                                this.dt_start.SelectedDate = today.AddMonths(3 - month);
                                this.dt_start.SelectedDate = this.dt_start.SelectedDate.Value.AddDays(1 - this.dt_start.SelectedDate.Value.Day);
                            }
                            else
                            if (month < 9)
                            {
                                this.dt_start.SelectedDate = today.AddMonths(6 - month);
                                this.dt_start.SelectedDate = this.dt_start.SelectedDate.Value.AddDays(1 - this.dt_start.SelectedDate.Value.Day);
                            }
                            else
                            if (month < 12)
                            {
                                this.dt_start.SelectedDate = today.AddMonths(9 - month);
                                this.dt_start.SelectedDate = this.dt_start.SelectedDate.Value.AddDays(1 - this.dt_start.SelectedDate.Value.Day);
                            }
                            this.dt_end.SelectedDate = today;
                        }; break;
                    case "normal":
                        {
                            this.dt_start.SelectedDate = today.AddDays(-today.Day);
                            this.dt_end.SelectedDate = today;
                        }; break;
                }
            }
            catch (Exception e1)
            {
                //MessageBox.Show("切换日期失败，错误信息:" + e1.Message);
            }
        }
    }
}
